package com.yit.test.demo;

import java.math.BigDecimal;

import com.yit.test.demo.Entity.OrderInfo;
import com.yit.test.demo.Entity.PriceInfo;
import com.yit.test.demo.Entity.ProductInfo;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.when;
import static org.testng.Assert.*;

/**
 * Created by zhangyanfei on 2017/8/31.
 *
 * @author zhangyanfei
 * @date 2017/08/31
 */
public class OrderServiceTestWithMock {
    @Mock
    private ProductService productService;
    @Mock
    private PriceService priceService;
    @InjectMocks
    private OrderService orderService;
    @BeforeClass
    public void setUp(){
        MockitoAnnotations.initMocks(this);
        setMockData();
    }

    private void setMockData() {
        when(priceService.getPriceInfoByProductCode(anyString())).thenAnswer(invoke->{
                PriceInfo priceInfo=new PriceInfo();
                priceInfo.price= BigDecimal.valueOf(100);
                return priceInfo;
        }
        );

        when(productService.getProductInfoByProductCode(anyString())).thenAnswer(invocation -> {
            ProductInfo productInfo=new ProductInfo();
            productInfo.optionText="10支装";
            productInfo.productCode="L3001";
            productInfo.productName="mock商品";
            return productInfo;
        });
    }

    @Test
    public void testGetOrderInfoByOrderCode() throws Exception {
       OrderInfo orderInfo=orderService.getOrderInfoByOrderCode("Test");
       Assert.assertNotNull(orderInfo.optionText);
    }

}